using System.Collections.Generic;
using System.Reflection;
using UnityEditor;

#if UNITY_EDITOR
using UnityEngine;
#endif

namespace IQIGame.Onigao.GamePlay
{

    /// <summary>
    /// GamePlay统一使用的PlayerPrefs常量，避免Key重复
    /// 由于部分程序集如HotUpdate也使用了少量PlayerPrefs，这些程序集无法调用PlayerPrefsConst
    /// 所以为了尽可能保证不重复，该类所有value都加上Game_的前缀
    /// </summary>
#if UNITY_EDITOR
    [InitializeOnLoad]
#endif
    public static class PlayerPrefsConst
    {
        #region runtime
        public const string CacheGMCmd = "Game_CacheGMCmd";
        public const string CacheGMCmdArgs = "Game_CacheGMCmdArgs";
        public const string GMHistory = "Game_GMHistory";
        public const string LoginUserName = "Game_LoginUserName";
        public const string LoginPassword = "Game_LoginPassword";
        public const string DevelopSetting = "Game_DevelopSetting";
        public const string SettingGame = "Game_SettingGame";
        public const string SettingAudio = "Game_SettingAudio";
        #endregion

        #region Editor
        public const string EditorIsLevelFreeCamera = "Game_EditorIsLevelFreeCamera";
        public const string EditorActiveNavMeshPreview = "Game_EditorActiveNavMeshPreview";
        public const string EditorActiveLogicNavMeshPreview = "Game_EditorActiveLogicNavMeshPreview";
        public const string EditorEnabledMsgDispatcherTrackerAutoReload = "Game_EditorEnabledMsgDispatcherTrackerAutoReload";
        public const string EditorEntityGenBindingEnumNames = "Game_EditorEntityGenBindingEnumNames";
        public const string EditorDebugTeleportPos0 = "Game_EditorDebugTeleportPos0";
        public const string EditorDebugTeleportPos1 = "Game_EditorDebugTeleportPos1";
        public const string EditorDebugTeleportPos2 = "Game_EditorDebugTeleportPos2";
        public const string GameEnterMode = "Game_GameEnterMode";
        public const string LevelEditorRegionCid = "Game_LevelEditorRegionCid";
        public const string LevelEditorIsLoadBlocks = "Game_LevelEditorIsLoadBlocks";
        public const string MainCityEditorRegionCid = "Game_MainCityEditorRegionCid";
        public const string MainCityEditorIsLoadBlocks = "Game_MainCityEditorIsLoadBlocks";
        #endregion

        #region 检查
#if UNITY_EDITOR
        static PlayerPrefsConst()
        {
            HashSet<string> keys = new();
            FieldInfo[] fields = typeof(PlayerPrefsConst).GetFields(BindingFlags.Public | BindingFlags.Static);
            foreach (FieldInfo field in fields)
            {
                if (field.IsLiteral && !field.IsInitOnly)
                {
                    if (field.FieldType != typeof(string))
                    {
                        Debug.LogError($"PlayerPrefsConst.{field.Name}不是string类型。");
                        return;
                    }
                    else
                    {
                        var value = field.GetValue(null) as string;
                        if (keys.Contains(value))
                        {
                            Debug.LogError($"PlayerPrefsConst.{field.Name}的值{value}和其他字段重复。");
                            return;
                        }
                        keys.Add(value);
                    }
                }
            }
        }
#endif
        #endregion
    }
}
